﻿using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CubicFWNet.Application.SystemManager
{
    public class NewsApp
    {
        private NewsRepository _newsRepository = new NewsRepository();
        private UserRepository _usersRepository = new UserRepository();
        public IEnumerable<NewsView> GetLearningList()
        {
            return _newsRepository.GetList().OrderByDescending(a => a.CreateTime);
        }
        public int AddOrUpdate(News news)
        {
            if (news.Id == Guid.Empty)
            {
                news.Id = Guid.NewGuid();
                return _newsRepository.Insert(news);
            }
            else
            {
                return _newsRepository.Update(news);
            }
        }
        public News GetForm(Guid id)
        {
            return _newsRepository.FindEntity(a => a.Id == id);
        }
        public int Delete(Guid id)
        {
            var del = _newsRepository.FindEntity(u => u.Id == id);
            if (del == null) return -404;
            return _newsRepository.Delete(a => a.Id == id);
        }
        public User GetId(String name) {
           User u = _usersRepository.FindEntity(a => a.UserName == name);
           return u;
        }
        public int Update(News news)
        {
            return _newsRepository.Update(news);
        }
       
    }

}
